package com.gxa.user.controller.upload;

import com.gxa.o2o.core.dto.ResultDTO;
import com.gxa.user.service.UserService;
import com.gxa.user.utils.StringUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author 阔爱又迷人的川锅锅呀
 */
@RestController
@RequestMapping("/")
@Api("图片上传接口")
@ApiResponses({
        @ApiResponse(code = 400, message = "请求参数不完整或者错误"),
        @ApiResponse(code = 404, message = "找不到页面"),
        @ApiResponse(code = 405, message = "请求方式不正确，比如后台接收是post，但前端请求的是get"),
        @ApiResponse(code = 406, message = "页面地址和接口名冲突"),
        @ApiResponse(code = 415, message = "请求格式不正确，前端可能没有把请求头修改为json，也可能前端请求的json对象没有转换为字符串"),
        @ApiResponse(code = 500, message = "后台服务逻辑错误")
})
public class UploadController {
   @Resource(name = "userService")
    private UserService userService;

   @PostMapping("/upload")
   @ApiImplicitParams({
           @ApiImplicitParam(name = "file",value = "图片对象"),
           @ApiImplicitParam(name = "folder",value = "存放文件夹名称")
   })
   @ApiOperation(value = "图片上传接口",notes = "图片上传",httpMethod = "POST")
  public   ResultDTO upload(@RequestParam("file") MultipartFile file, String folder) throws IOException {
       /*
        *获取文件名
        */
       String fileName = StringUtil.getExt(file.getName());
       /*
        *通过文件后缀判断文件格式
        */
       Boolean ext = StringUtil.checkExt("jpg|JPG|jpeg|JPEG|png|PNG|gif|GIF", fileName);
       if (ext) {
           return  userService.upload(file,folder);
       }
       return new ResultDTO(1001, "文件格式不符合要求");
   }

}
